/*
 * Copyright (c) 2019 Mike Penz
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mikepenz.iconics.view;

import ohos.agp.components.AbsButton;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Checkbox;
import ohos.agp.components.element.Element;
import ohos.app.Context;
import ohos.media.image.common.Size;

import com.mikepenz.iconics.IconicsDrawable;
import com.mikepenz.iconics.IconicsDrawableConverter;
import com.mikepenz.iconics.internal.IconicsViewsAttrsApplier;

/**
 * 带状态图标的checkbox
 *
 * @author pa.gulko zTrap
 * @since 2017-06-07
 */
public class IconicsCompoundButton extends Checkbox {
    private Element checkedIcon;
    private Element unCheckedIcon;

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param attrSet 属性
     */
    public IconicsCompoundButton(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init(context, attrSet);
    }

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param attrSet 属性
     * @param styleName 样式
     */
    public IconicsCompoundButton(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init(context, attrSet);
    }

    private void init(Context context, AttrSet attrSet) {
        IconicsDrawable checkedDrawable = IconicsViewsAttrsApplier.getIconicsCheckedDrawable(context, attrSet);
        setCheckedIcon(checkedDrawable);
        IconicsDrawable unCheckedDrawable = IconicsViewsAttrsApplier.getIconicsUnCheckedDrawable(context, attrSet);
        setUnCheckedIcon(unCheckedDrawable);
        setCheckedStateChangedListener(new CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean isChecked) {
                if (isChecked) {
                    setButtonElement(checkedIcon);
                } else {
                    setButtonElement(unCheckedIcon);
                }
            }
        });
        setButtonElement(unCheckedIcon);
    }

    /**
     * 设置选中的icon
     *
     * @param icon 图标
     */
    public void setCheckedIcon(IconicsDrawable icon) {
        checkedIcon = IconicsDrawableConverter.converter(icon, new Size(icon.getSizeXpx(), icon.getSizeYpx()));
    }

    /**
     * 设置未被选中的icon
     *
     * @param icon 图标
     */
    public void setUnCheckedIcon(IconicsDrawable icon) {
        unCheckedIcon = IconicsDrawableConverter.converter(icon, new Size(icon.getSizeXpx(), icon.getSizeYpx()));
    }

    @Override
    public String getAccessibilityDescription() {
        return IconicsCompoundButton.class.getName();
    }
}
